package com.fzu.normalservice.controller;


import com.fzu.commonutil.entity.User;
import com.fzu.commonutil.mapper.UserMapper;
import com.fzu.commonutil.params.DataResult;
import com.fzu.commonutil.util.*;
import com.fzu.normalservice.service.CompanyService;
import com.fzu.normalservice.service.DemandService;
import com.fzu.normalservice.service.HousekeeperService;
import com.fzu.normalservice.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyq
 * @since 2023-03-26
 */
@RestController
@RequestMapping("/data")
@RequiredArgsConstructor
public class DataController {
    private final RedisUtil redisUtil;
    /**
     * 柱状图 最近一周用户数变化图
     * 折线图 最近六个月 需求数/订单数 趋势图
     * 饼状图 最近六个月 各类服务订单饼状图
     * 总订单数/总需求数/总用户数/总公司数/总家政员数
     *
     * @return
     */
    @GetMapping
    public R data(@RequestHeader("token") String token) {
        User user = JwtUtil.getUserFromRedis(token);
        Integer role = user.getRole();
        Integer companyId = user.getCompanyId();
        DataResult orderData = new DataResult();
        DataResult normalData = new DataResult();
        if (CodeEnum.MANAGER.getCode().equals(role)) {
            orderData = (DataResult)redisUtil.get(RedisKey.ORDER_DATA);
            normalData = (DataResult)redisUtil.get(RedisKey.NORMAL_DATA);
        } else if (CodeEnum.COMPANY.getCode().equals(role)){
            orderData = (DataResult)redisUtil.get(RedisKey.ORDER_DATA + "_" + companyId);
            normalData = (DataResult)redisUtil.get(RedisKey.NORMAL_DATA + "_" + companyId);
        }
        normalData.setTypeData(orderData.getTypeData());
        normalData.setTypeLabel(orderData.getTypeLabel());
        normalData.setOrderCount(orderData.getOrderCount());
        normalData.setOrderData(orderData.getOrderData());
        normalData.setOrderLabel(orderData.getOrderLabel());
        return R.ok().data(normalData);
    }
}

